

<template>
  <div name="医生签名box" class="bold color-0 sign4-html">
    <span>{{ info.label }}</span>
    <div class="sing-box hw200 pointer" @click="open_dialog(1)">

      <span v-for="(item, index) in urlList" v-show="!!info.field_value" :key="index">
        <img :src="item.URL" class="sign-img middle" />
        <i class="el-icon-delete color-danger middle" @click.stop="deleteQM(index)" v-show="pdf != 1"></i>
      </span>
    </div>

    <el-dialog title="输入HIS账号、二级密码获取签名" :visible.sync="dialogVisible" width="450px" :close-on-click-modal="false">
      <div>
        HIS账号：
        <el-input v-model.trim="YHZH" @keyup.native="upperUsername()"></el-input>

        <div class="mt20">
          二级密码：
        </div>
        <el-input v-model.trim="MM" type="password"></el-input>

      </div>
      <span slot="footer" class="dialog-footer">
        <el-checkbox type="checkbox" class="mr30" v-model="checked">使用登录账号签名</el-checkbox>
        <el-button @click="dialogVisible = false">关闭</el-button>
        <el-button type="primary" @click="sureInfo()">确 定</el-button>
      </span>
    </el-dialog>


  </div>
</template>

<script>
import { timeFormatter } from "UTILS/format";
import { simDeepCopy, getStorage, setStorage } from "UTILS/util";
import { CONSENT_GET_DOCTOR_DZQM_BY_YHMM } from "_SERVICES/api";

import md5 from 'md5'

export default {
  name: "Sign4",
  props: {
    info: {
    },
    TPL_FORMAT: {

    },
    pdf: {}
  },
  data() {
    return {
      urlList: [],
      dialogVisible: false,
      YHZH: "",
      MM: "",
      checked: false,
      qm_url:"http://10.1.6.81/dzqm/dzqm.aspx"
    };
  },
  created() {
    if(window.location.href.indexOf("https")>-1){
      this.qm_url = "https://dzqm360.sysush.com/dzqm/dzqm.aspx"
    }
  },
  watch: {
    info: {
      handler(val) {
        console.log(val)
        if (!val.field_value) {
          this.urlList = []
        } else {
          if (!this.urlList.length) {
            this.init_sign();
          }
        }
      },
      deep: true, // 使用 deep 选项进行深度监视
      immediate: true,
    },
  },
  destroyed() { },
  methods: {
    //初始化渲染签名
    init_sign() {
      this.urlList = [];
      this.info.field_value.split(",").forEach((item) => {
        this.urlList.push({
          URL: `${this.qm_url}?as_yhid=${item}`,
          YHID: item
        })
      })
    },
    open_dialog() {
      if(this.pdf==1){
        return false;
      }
      this.dialogVisible = true
    },
    sureInfo() {

      if (this.checked == false && (!this.YHZH || !this.MM)) {
        this.$message.error("请输入账号和二级密码")
        return false;
      }

      if (this.checked == true) {
        let userInfo = getStorage("cst_userInfo") ? JSON.parse(getStorage("cst_userInfo")) : {}
        this.setQM(userInfo.YHID)
        this.resetInfo()
      } else {
        //发送请求给后端
        this.get_yhid();
      }
      console.log(this.YHZH)
      console.log(this.MM)
    },
    resetInfo(){
      this.dialogVisible = false;
      this.checked = false;
      this.YHZH = "";
      this.MM = "";
    },
    upperUsername() {
      this.YHZH = this.YHZH.toUpperCase()
    },
    get_yhid() {
      CONSENT_GET_DOCTOR_DZQM_BY_YHMM({
        "TYPE": "2",  // 1 表示密码，2 表示二级密码
        "YHZH": this.YHZH,
        "YHMM": md5(this.MM).toUpperCase()
      }).then((res)=>{
        this.setQM(res.data.YHID)
        this.resetInfo()
      })
    },
    //设置签名数据
    setQM(YHID) {
      this.urlList.push({
        URL: `${this.qm_url}?as_yhid=${YHID}`,
        YHID: YHID
      });
      if (this.info.field_value) {
        this.info.field_value += "," + YHID
      } else {
        this.info.field_value = YHID
      }
      if (this.info.field_related) {
        this.set_related_time();
      }
    },
    // BeginSign() {
    //   CONSENT_GET_DOCTOR_DZQM({
    //     YHID: userInfo.YHID
    //   }).then((res) => {
    //     console.log("获取医生签名4")
    //     let url = res.data.dzqm_url;
    //     this.urlList.push({
    //       URL: url,
    //       YHID: userInfo.YHID
    //     });
    //     if (this.info.field_value) {
    //       this.info.field_value += "," + userInfo.YHID
    //     } else {
    //       this.info.field_value = userInfo.YHID
    //     }
    //     if (this.info.field_related) {
    //       this.set_related_time();
    //     }
    //   })
    // },
    deleteQM(index) {
      this.urlList.splice(index, 1);
      let yhids = ""
      this.urlList.forEach((item) => {

        if (yhids) {
          yhids += "," + item.YHID
        } else {
          yhids = item.YHID
        }
      })
      this.info.field_value = yhids;
      console.log(this.info.field_value)

    },
    set_related_time() {
      if (this.TPL_FORMAT) {
        this.TPL_FORMAT.forEach((item) => {
          if (item.field_name == this.info.field_related) {
            item.field_value = timeFormatter(new Date(), 4)
          }
        })
      }
    }
  },
};
</script>

<style  lang="less">
.sign4-html {
  margin-top: 20px;

  .sing-box {
    display: inline-block;
    min-height: 40px;
    vertical-align: middle;

  }

  .sign-img {
    // position: absolute;
    // left: 20px;
    // width: 100px;
    max-width: 80px;
    height: 40px;
    // z-index: 1;
  }

}
</style>

